import warnings
warnings.warn("qturboframe.public_ui.ufc_wpf.input_type模块已被废弃，替代模块为qturboframe.public_ui.ucf_wpf.input_type", DeprecationWarning)

from qturboframe.lib.utils.by import By
from qturboframe.lib.utils.controlname import ControlName, ControlType
from qturboframe.lib.ranorex.exceptions import CsClientError
from qturboframe.public_ui.ufc_wpf.basecontrol import BaseControl
from qturboframe.lib.ranorex.win_control.text import Text as WinText
from qturboframe.public_ui.ufc_wpf.utili.common import Common


class InputType(BaseControl):
    """
    ucf wpf的输入框类

    :Attributes:
     - _driver: 控件所在的当前Control对象
     - _control: 控件元素Control对象
     - input_type: 当前的InputType对象
     - timeout: 步骤操作的超时时间

     :Raises
        CsClientError 没有找到控件异常

    :Example:
     - 例1: InputType(control)
     - 例2: InputType({By.RANOREXPATH:'ranorexpath'})
     - 例3: InputType({By.TEXT:'导入'})
     - 例4: InputType({By.XPATH:'xpath','text':'文本信息'})
     - 例5: InputType({By.AUTOMATIONID:'automationid','text':'文本信息'}, father=driver)
    """

    def __init__(self, element_para, father=None, timeout=10000):
        if isinstance(element_para, dict) and By.LABEL_TEXT in element_para:
            label_text = element_para.get(By.LABEL_TEXT)
            label_text = Common.update_special_character(label_text)
            #text标签文字为label_text, 类型为TextBlock并且可见
            xpath = ".//text[@Text~'{}' and @automationid='PART_Label' and @Type='TextBlock']".format(label_text)
            control = father.find_first_visible_element(By.XPATH, xpath, timeout=timeout)
            if control:
                xpath = "./ancestor::text[@Type='{}']".format(ControlType.HSTEXTBOX)
                super().__init__({By.XPATH: xpath},  control, timeout)
            else:
                raise CsClientError("请确定按照此Label文字:{}可以找到对应的控件".format(label_text))
        else:
            super().__init__(element_para, father, timeout)

        if self._control.get_control_name() == ControlName.TEXTBOXVIEW or self._control.get_attribute_value("Type") == ControlName.TEXTBOXVIEW:
            self.input_type = WinText(self._control.control)
        else:
            xpath = ".//text[@Type='TextBoxView']"
            control = self._control.exist(By.XPATH, xpath, timeout=timeout)
            self.input_type = WinText(control.control)

    def set_value(self, value):
        '''
        ucf文本框填充

        :Args:
         - value: 填充值

        :Example:
         - 例: input_type({By.TEXT: "Height(控件高度)"}).set_value("400")
        '''
        self.clear()
        self.input_type.focus()
        self.input_type.send_keys(value)

    def clear(self):
        '''
        ucf文本框清空

        :Usage:
         - input_type.clear()
        '''
        self.input_type.focus()
        self.input_type.send_keys('')

    def send_keys_with_no_clear(self, value):
        '''
        ucf给文本框中输入信息并且不清楚之前信息

        :Args:
         - value: 填充值

        :Example:
         - 例: input_type({By.TEXT: "Height(控件高度)"}).send_keys_with_no_clear("400")
        '''
        self.input_type.click()
        self.input_type.press_keys(value)